import { getUserInfo, loginAuth } from '@/apis'
import { defineStore } from 'pinia'
import { useRequest } from './useRequest'
export const useStore = defineStore('main', {
  state: () => {
    return {
      token: '' as undefined | string,
      userInfo: {} as any
    }
  },
  actions: {
    actionLogout() {
      // logoutAuth()
      this.$reset()
      uni.clearStorage()
      uni.redirectTo({ url: '/pages/login/login' })
    },
    initToken() {
      const token = uni.getStorageSync('token')
      if(token) this.token = token
    },
    async actionLogin(params: { username: string, password: string }) {
      const { data, run } = useRequest([loginAuth, params], { manual: true })
      await run()
      this.token = data.value?.satoken
      uni.setStorageSync('token', this.token)
      uni.switchTab({ url: '/pages/index/index' });
    },
    async flushUserInfo() {
      const { data: userInfo, run: runUserInfo } = useRequest([getUserInfo], { manual: true })
      await runUserInfo()
      this.userInfo = userInfo.value
    }
  }
})